package com.bestvike.stone.core.serialization.fastjson;

import com.alibaba.fastjson.parser.DefaultJSONParser;
import com.alibaba.fastjson.parser.deserializer.MapDeserializer;
import com.bestvike.stone.core.reflect.ReflectionUtils;

import java.lang.reflect.Type;
import java.util.Map;

/**
 * Created by 许崇雷 on 2018-02-26.
 */
public final class StringObjectMapDeserializer extends MapDeserializer {
    public static final StringObjectMapDeserializer instance = new StringObjectMapDeserializer();

    private StringObjectMapDeserializer() {
    }

    @Override
    @SuppressWarnings("unchecked")
    protected Object deserialze(DefaultJSONParser parser, Type type, Object fieldName, Map map) {
        Object value = parseMap(parser, map, Object.class, fieldName);
        try {
            ReflectionUtils.setFieldValue(value, "type", type);
        } catch (Exception ignored) {
        }
        return value;
    }
}
